//
//  FlyAwayViewController.m
//  FlyAway
//
//  Created by Maxime Britto on 20/04/10.
//  Copyright Logimax SARL 2010. All rights reserved.
//

#import "FlyAwayViewController.h"

#import "Obstacle.h"
#import "Hero.h"
#import "Constantes.h"

@implementation FlyAwayViewController

- (float)valeurAleatoireCompriseEntre:(float)borne1 et:(float)borne2
{
	return (rand() * (borne2 - borne1) / RAND_MAX) + borne1;
}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    monHero = [[Hero alloc]initWithPosition:CGPointMake(0, 0)];
    [self.view addSubview:monHero];
	[monHero moveHome:NO];
    [self placerObstaclesAleatoirement:9];
    
    //[NSTimer scheduledTimerWithTimeInterval:[self valeurAleatoireCompriseEntre:2 et:4] target:self selector:@selector(deplacerObstaclesAleatoirement) userInfo:nil repeats:YES];
    
}

- (void)placerObstaclesAleatoirement:(int)nbObstacles
{
    listeObstacles = [[NSMutableArray alloc] init];
	for (int i = 0; i < nbObstacles; i++) {
		float x = [self valeurAleatoireCompriseEntre:(float)0 et:(float)LARGEUR_ECRAN-LARGEUR_OBSTACLE];
        float y =[self valeurAleatoireCompriseEntre:(float)HAUTEUR_ARRIVEE et:(float)HAUTEUR_ECRAN-HAUTEUR_OBSTACLE];
		Obstacle* newObstacle = [[Obstacle alloc] initWithPosition:CGPointMake(x, y)];
		[listeObstacles addObject:newObstacle];
		[self.view addSubview:newObstacle];
        
	}
}

- (void)deplacerObstaclesAleatoirement{
    for(Obstacle* o in listeObstacles){
     
        CGPoint c = o.center;
        c.x -= [self valeurAleatoireCompriseEntre:1 et:50];
        c.y -= [self valeurAleatoireCompriseEntre:1 et:20];
        
        [o moveCenterToPosition:c animated:YES];    
    }
}

- (void)partieTerminee
{
	[monHero moveHome:YES];
	if (monHero.isMoving) {
		monHero.isMoving = NO;
	}
}

-  (void)partieGagnee
{
	messageHaut.text = @"Gagné!";
	[self partieTerminee];
    [self rejouerPartie];
}

- (void)partiePerdue
{
	messageHaut.text = @"Perdu";
	[self partieTerminee];
}

- (void)rejouerPartie{
    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:nil message:@"Voulez vous rejouer?" delegate:self cancelButtonTitle:@"Oui" otherButtonTitles:@"Non", @"lol",  nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
        messageHaut.text = @"Arrivée";
    else if (buttonIndex == 1)
        messageHaut.text = @"false";
    else
        messageHaut.text = @"lol";
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	CGPoint position = [touch locationInView:self.view];
    if (CGRectContainsPoint(monHero.frame, position)){
        monHero.isMoving = YES;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [touches anyObject];
	CGPoint position = [touch locationInView:self.view];
	if (monHero.isMoving == YES) {
		[monHero moveCenterToPosition:position animated:YES];
        
		//Verification des obstacles
        
        if([monHero collisionTest:listeObstacles]){
            [self partiePerdue];
        }
        
        /*
        for(Obstacle* o in listeObstacles){
            if([o estProcheDuPoint:CGPointMake(monHero.center.x, monHero.center.y)])
                NSLog(@"touche");
        }*/
        
        
		//verification gagné
		if(monHero.center.y + HAUTEUR_HERO <= HAUTEUR_ARRIVEE)
            [self partieGagnee];
        
	}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (monHero.isMoving) {
		monHero.isMoving = NO;
	}
}



@end
